
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>嵌套类型 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../../../../../0/style.css">

    
            
                
                <link rel="stylesheet" href="../../../../../../0/website.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../0/search.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../0/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="20_Extensions.html" />
    
    
    <link rel="prev" href="18_Type_Casting.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    欢迎使用 Swift
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../chapter1/01_about_swift.html">
            
                <a href="../chapter1/01_about_swift.html">
            
                    
                    关于 Swift
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../chapter1/02_version_compatibility.html">
            
                <a href="../chapter1/02_version_compatibility.html">
            
                    
                    版本兼容性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../chapter1/03_a_swift_tour.html">
            
                <a href="../chapter1/03_a_swift_tour.html">
            
                    
                    Swift 初见
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../chapter1/04_revision_history.html">
            
                <a href="../chapter1/04_revision_history.html">
            
                    
                    Swift 版本历史记录
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    Swift 教程
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="01_The_Basics.html">
            
                <a href="01_The_Basics.html">
            
                    
                    基础部分
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="02_Basic_Operators.html">
            
                <a href="02_Basic_Operators.html">
            
                    
                    基本运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="03_Strings_and_Characters.html">
            
                <a href="03_Strings_and_Characters.html">
            
                    
                    字符串和字符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="04_Collection_Types.html">
            
                <a href="04_Collection_Types.html">
            
                    
                    集合类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="05_Control_Flow.html">
            
                <a href="05_Control_Flow.html">
            
                    
                    控制流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="06_Functions.html">
            
                <a href="06_Functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="07_Closures.html">
            
                <a href="07_Closures.html">
            
                    
                    闭包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="08_Enumerations.html">
            
                <a href="08_Enumerations.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="09_Structures_And_Classes.html">
            
                <a href="09_Structures_And_Classes.html">
            
                    
                    类和结构体
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.10" data-path="10_Properties.html">
            
                <a href="10_Properties.html">
            
                    
                    属性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.11" data-path="11_Methods.html">
            
                <a href="11_Methods.html">
            
                    
                    方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.12" data-path="12_Subscripts.html">
            
                <a href="12_Subscripts.html">
            
                    
                    下标
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.13" data-path="13_Inheritance.html">
            
                <a href="13_Inheritance.html">
            
                    
                    继承
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.14" data-path="14_Initialization.html">
            
                <a href="14_Initialization.html">
            
                    
                    构造过程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.15" data-path="15_Deinitialization.html">
            
                <a href="15_Deinitialization.html">
            
                    
                    析构过程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.16" data-path="16_Optional_Chaining.html">
            
                <a href="16_Optional_Chaining.html">
            
                    
                    可选链
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.17" data-path="17_Error_Handling.html">
            
                <a href="17_Error_Handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.18" data-path="18_Type_Casting.html">
            
                <a href="18_Type_Casting.html">
            
                    
                    类型转换
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.19" data-path="19_Nested_Types.html">
            
                <a href="19_Nested_Types.html">
            
                    
                    嵌套类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.20" data-path="20_Extensions.html">
            
                <a href="20_Extensions.html">
            
                    
                    扩展
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.21" data-path="21_Protocols.html">
            
                <a href="21_Protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.22" data-path="22_Generics.html">
            
                <a href="22_Generics.html">
            
                    
                    泛型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.23" data-path="23_Automatic_Reference_Counting.html">
            
                <a href="23_Automatic_Reference_Counting.html">
            
                    
                    自动引用计数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.24" data-path="24_Memory_Safety.html">
            
                <a href="24_Memory_Safety.html">
            
                    
                    内存安全
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.25" data-path="25_Access_Control.html">
            
                <a href="25_Access_Control.html">
            
                    
                    访问控制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.26" data-path="26_Advanced_Operators.html">
            
                <a href="26_Advanced_Operators.html">
            
                    
                    高级运算符
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    语言参考
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter3/01_About_the_Language_Reference.html">
            
                <a href="../chapter3/01_About_the_Language_Reference.html">
            
                    
                    关于语言参考
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter3/02_Lexical_Structure.html">
            
                <a href="../chapter3/02_Lexical_Structure.html">
            
                    
                    词法结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../chapter3/03_Types.html">
            
                <a href="../chapter3/03_Types.html">
            
                    
                    类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../chapter3/04_Expressions.html">
            
                <a href="../chapter3/04_Expressions.html">
            
                    
                    表达式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../chapter3/05_Statements.html">
            
                <a href="../chapter3/05_Statements.html">
            
                    
                    语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../chapter3/06_Declarations.html">
            
                <a href="../chapter3/06_Declarations.html">
            
                    
                    声明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../chapter3/07_Attributes.html">
            
                <a href="../chapter3/07_Attributes.html">
            
                    
                    特性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="../chapter3/08_Patterns.html">
            
                <a href="../chapter3/08_Patterns.html">
            
                    
                    模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="../chapter3/09_Generic_Parameters_and_Arguments.html">
            
                <a href="../chapter3/09_Generic_Parameters_and_Arguments.html">
            
                    
                    泛型参数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.10" data-path="../chapter3/10_Summary_of_the_Grammar.html">
            
                <a href="../chapter3/10_Summary_of_the_Grammar.html">
            
                    
                    语法总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    翻译贡献者
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../contributors.html">
            
                <a href="../contributors.html">
            
                    
                    翻译贡献者
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >嵌套类型</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x5D4C;&#x5957;&#x7C7B;&#x578B;">&#x5D4C;&#x5957;&#x7C7B;&#x578B;</h1>
<p>&#x679A;&#x4E3E;&#x5E38;&#x88AB;&#x7528;&#x4E8E;&#x4E3A;&#x7279;&#x5B9A;&#x7C7B;&#x6216;&#x7ED3;&#x6784;&#x4F53;&#x5B9E;&#x73B0;&#x67D0;&#x4E9B;&#x529F;&#x80FD;&#x3002;&#x7C7B;&#x4F3C;&#x5730;&#xFF0C;&#x679A;&#x4E3E;&#x53EF;&#x4EE5;&#x65B9;&#x4FBF;&#x7684;&#x5B9A;&#x4E49;&#x5DE5;&#x5177;&#x7C7B;&#x6216;&#x7ED3;&#x6784;&#x4F53;&#xFF0C;&#x4ECE;&#x800C;&#x4E3A;&#x67D0;&#x4E2A;&#x590D;&#x6742;&#x7684;&#x7C7B;&#x578B;&#x6240;&#x4F7F;&#x7528;&#x3002;&#x4E3A;&#x4E86;&#x5B9E;&#x73B0;&#x8FD9;&#x79CD;&#x529F;&#x80FD;&#xFF0C;Swift &#x5141;&#x8BB8;&#x4F60;&#x5B9A;&#x4E49;<em>&#x5D4C;&#x5957;&#x7C7B;&#x578B;</em>&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x652F;&#x6301;&#x7684;&#x7C7B;&#x578B;&#x4E2D;&#x5B9A;&#x4E49;&#x5D4C;&#x5957;&#x7684;&#x679A;&#x4E3E;&#x3001;&#x7C7B;&#x548C;&#x7ED3;&#x6784;&#x4F53;&#x3002;</p>
<p>&#x8981;&#x5728;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x4E2D;&#x5D4C;&#x5957;&#x53E6;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#xFF0C;&#x5C06;&#x5D4C;&#x5957;&#x7C7B;&#x578B;&#x7684;&#x5B9A;&#x4E49;&#x5199;&#x5728;&#x5176;&#x5916;&#x90E8;&#x7C7B;&#x578B;&#x7684; <code>{}</code> &#x5185;&#xFF0C;&#x800C;&#x4E14;&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x9700;&#x8981;&#x5B9A;&#x4E49;&#x591A;&#x7EA7;&#x5D4C;&#x5957;&#x3002;</p>
<h2 id="nested-types-in-action">&#x5D4C;&#x5957;&#x7C7B;&#x578B;&#x5B9E;&#x8DF5; </h2>
<p>&#x4E0B;&#x9762;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x7ED3;&#x6784;&#x4F53; <code>BlackjackCard</code>&#xFF08;&#x4E8C;&#x5341;&#x4E00;&#x70B9;&#xFF09;&#xFF0C;&#x7528;&#x6765;&#x6A21;&#x62DF; <code>BlackjackCard</code> &#x4E2D;&#x7684;&#x6251;&#x514B;&#x724C;&#x70B9;&#x6570;&#x3002;<code>BlackjackCard</code> &#x7ED3;&#x6784;&#x4F53;&#x5305;&#x542B;&#x4E24;&#x4E2A;&#x5D4C;&#x5957;&#x5B9A;&#x4E49;&#x7684;&#x679A;&#x4E3E;&#x7C7B;&#x578B; <code>Suit</code> &#x548C; <code>Rank</code>&#x3002;</p>
<p>&#x5728; <code>BlackjackCard</code> &#x4E2D;&#xFF0C;<code>Ace</code> &#x724C;&#x53EF;&#x4EE5;&#x8868;&#x793A; <code>1</code> &#x6216;&#x8005; <code>11</code>&#xFF0C;<code>Ace</code> &#x724C;&#x7684;&#x8FD9;&#x4E00;&#x7279;&#x5F81;&#x901A;&#x8FC7;&#x4E00;&#x4E2A;&#x5D4C;&#x5957;&#x5728; <code>Rank</code> &#x679A;&#x4E3E;&#x4E2D;&#x7684;&#x7ED3;&#x6784;&#x4F53; <code>Values</code> &#x6765;&#x8868;&#x793A;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">BlackjackCard</span> </span>{

    <span class="hljs-comment">// &#x5D4C;&#x5957;&#x7684; Suit &#x679A;&#x4E3E;</span>
    <span class="hljs-class"><span class="hljs-keyword">enum</span> <span class="hljs-title">Suit</span>: <span class="hljs-title">Character</span> </span>{
        <span class="hljs-keyword">case</span> spades = <span class="hljs-string">&quot;&#x2660;&quot;</span>, hearts = <span class="hljs-string">&quot;&#x2661;&quot;</span>, diamonds = <span class="hljs-string">&quot;&#x2662;&quot;</span>, clubs = <span class="hljs-string">&quot;&#x2663;&quot;</span>
    }

    <span class="hljs-comment">// &#x5D4C;&#x5957;&#x7684; Rank &#x679A;&#x4E3E;</span>
    <span class="hljs-class"><span class="hljs-keyword">enum</span> <span class="hljs-title">Rank</span>: <span class="hljs-title">Int</span> </span>{
        <span class="hljs-keyword">case</span> two = <span class="hljs-number">2</span>, three, four, five, six, seven, eight, nine, ten
        <span class="hljs-keyword">case</span> jack, queen, king, ace
        <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Values</span> </span>{
            <span class="hljs-keyword">let</span> first: <span class="hljs-type">Int</span>, second: <span class="hljs-type">Int</span>?
        }
        <span class="hljs-keyword">var</span> values: <span class="hljs-type">Values</span> {
            <span class="hljs-keyword">switch</span> <span class="hljs-keyword">self</span> {
            <span class="hljs-keyword">case</span> .ace:
                <span class="hljs-keyword">return</span> <span class="hljs-type">Values</span>(first: <span class="hljs-number">1</span>, second: <span class="hljs-number">11</span>)
            <span class="hljs-keyword">case</span> .jack, .queen, .king:
                <span class="hljs-keyword">return</span> <span class="hljs-type">Values</span>(first: <span class="hljs-number">10</span>, second: <span class="hljs-literal">nil</span>)
            <span class="hljs-keyword">default</span>:
                <span class="hljs-keyword">return</span> <span class="hljs-type">Values</span>(first: <span class="hljs-keyword">self</span>.rawValue, second: <span class="hljs-literal">nil</span>)
            }
        }
    }

    <span class="hljs-comment">// BlackjackCard &#x7684;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;</span>
    <span class="hljs-keyword">let</span> rank: <span class="hljs-type">Rank</span>, suit: <span class="hljs-type">Suit</span>
    <span class="hljs-keyword">var</span> description: <span class="hljs-type">String</span> {
        <span class="hljs-keyword">var</span> output = <span class="hljs-string">&quot;suit is <span class="hljs-subst">\(suit.rawValue)</span>,&quot;</span>
        output += <span class="hljs-string">&quot; value is <span class="hljs-subst">\(rank.values.first)</span>&quot;</span>
        <span class="hljs-keyword">if</span> <span class="hljs-keyword">let</span> second = rank.values.second {
            output += <span class="hljs-string">&quot; or <span class="hljs-subst">\(second)</span>&quot;</span>
        }
        <span class="hljs-keyword">return</span> output
    }
}
</code></pre>
<p><code>Suit</code> &#x679A;&#x4E3E;&#x7528;&#x6765;&#x63CF;&#x8FF0;&#x6251;&#x514B;&#x724C;&#x7684;&#x56DB;&#x79CD;&#x82B1;&#x8272;&#xFF0C;&#x5E76;&#x7528;&#x4E00;&#x4E2A; <code>Character</code> &#x7C7B;&#x578B;&#x7684;&#x539F;&#x59CB;&#x503C;&#x8868;&#x793A;&#x82B1;&#x8272;&#x7B26;&#x53F7;&#x3002;</p>
<p><code>Rank</code> &#x679A;&#x4E3E;&#x7528;&#x6765;&#x63CF;&#x8FF0;&#x6251;&#x514B;&#x724C;&#x4ECE; <code>Ace</code>~<code>10</code>&#xFF0C;&#x4EE5;&#x53CA; <code>J</code>&#x3001;<code>Q</code>&#x3001;<code>K</code>&#xFF0C;&#x8FD9; <code>13</code> &#x79CD;&#x724C;&#xFF0C;&#x5E76;&#x7528;&#x4E00;&#x4E2A; <code>Int</code> &#x7C7B;&#x578B;&#x7684;&#x539F;&#x59CB;&#x503C;&#x8868;&#x793A;&#x724C;&#x7684;&#x9762;&#x503C;&#x3002;&#xFF08;&#x8FD9;&#x4E2A; <code>Int</code> &#x7C7B;&#x578B;&#x7684;&#x539F;&#x59CB;&#x503C;&#x672A;&#x7528;&#x4E8E; <code>Ace</code>&#x3001;<code>J</code>&#x3001;<code>Q</code>&#x3001;<code>K</code> &#x8FD9; <code>4</code> &#x79CD;&#x724C;&#x3002;&#xFF09;</p>
<p>&#x5982;&#x4E0A;&#x6240;&#x8FF0;&#xFF0C;<code>Rank</code> &#x679A;&#x4E3E;&#x5728;&#x5185;&#x90E8;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x5D4C;&#x5957;&#x7ED3;&#x6784;&#x4F53; <code>Values</code>&#x3002;&#x7ED3;&#x6784;&#x4F53; <code>Values</code> &#x4E2D;&#x5B9A;&#x4E49;&#x4E86;&#x4E24;&#x4E2A;&#x5C5E;&#x6027;&#xFF0C;&#x7528;&#x4E8E;&#x53CD;&#x6620;&#x53EA;&#x6709; <code>Ace</code> &#x6709;&#x4E24;&#x4E2A;&#x6570;&#x503C;&#xFF0C;&#x5176;&#x4F59;&#x724C;&#x90FD;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x6570;&#x503C;&#xFF1A;</p>
<ul>
<li><code>first</code> &#x7684;&#x7C7B;&#x578B;&#x4E3A; <code>Int</code></li>
<li><code>second</code> &#x7684;&#x7C7B;&#x578B;&#x4E3A; <code>Int?</code>&#xFF0C;&#x6216;&#x8005;&#x8BF4;&#x201C;&#x53EF;&#x9009; <code>Int</code>&#x201D;</li>
</ul>
<p><code>Rank</code> &#x8FD8;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x8BA1;&#x7B97;&#x578B;&#x5C5E;&#x6027; <code>values</code>&#xFF0C;&#x5B83;&#x5C06;&#x4F1A;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A; <code>Values</code> &#x7ED3;&#x6784;&#x4F53;&#x7684;&#x5B9E;&#x4F8B;&#x3002;&#x8FD9;&#x4E2A;&#x8BA1;&#x7B97;&#x578B;&#x5C5E;&#x6027;&#x4F1A;&#x6839;&#x636E;&#x724C;&#x7684;&#x9762;&#x503C;&#xFF0C;&#x7528;&#x9002;&#x5F53;&#x7684;&#x6570;&#x503C;&#x53BB;&#x521D;&#x59CB;&#x5316; <code>Values</code> &#x5B9E;&#x4F8B;&#x3002;&#x5BF9;&#x4E8E; <code>J</code>&#x3001;<code>Q</code>&#x3001;<code>K</code>&#x3001;<code>Ace</code> &#x8FD9;&#x56DB;&#x79CD;&#x724C;&#xFF0C;&#x4F1A;&#x4F7F;&#x7528;&#x7279;&#x6B8A;&#x6570;&#x503C;&#x3002;&#x5BF9;&#x4E8E;&#x6570;&#x5B57;&#x9762;&#x503C;&#x7684;&#x724C;&#xFF0C;&#x4F7F;&#x7528;&#x679A;&#x4E3E;&#x5B9E;&#x4F8B;&#x7684; <code>Int</code> &#x7C7B;&#x578B;&#x7684;&#x539F;&#x59CB;&#x503C;&#x3002;</p>
<p><code>BlackjackCard</code> &#x7ED3;&#x6784;&#x4F53;&#x62E5;&#x6709;&#x4E24;&#x4E2A;&#x5C5E;&#x6027;&#x2014;&#x2014;<code>rank</code> &#x4E0E; <code>suit</code>&#x3002;&#x5B83;&#x4E5F;&#x540C;&#x6837;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x8BA1;&#x7B97;&#x578B;&#x5C5E;&#x6027; <code>description</code>&#xFF0C;<code>description</code> &#x5C5E;&#x6027;&#x7528; <code>rank</code> &#x548C; <code>suit</code> &#x4E2D;&#x7684;&#x5185;&#x5BB9;&#x6765;&#x6784;&#x5EFA;&#x5BF9;&#x6251;&#x514B;&#x724C;&#x540D;&#x5B57;&#x548C;&#x6570;&#x503C;&#x7684;&#x63CF;&#x8FF0;&#x3002;&#x8BE5;&#x5C5E;&#x6027;&#x4F7F;&#x7528;&#x53EF;&#x9009;&#x7ED1;&#x5B9A;&#x6765;&#x68C0;&#x67E5;&#x53EF;&#x9009;&#x7C7B;&#x578B; <code>second</code> &#x662F;&#x5426;&#x6709;&#x503C;&#xFF0C;&#x82E5;&#x6709;&#x503C;&#xFF0C;&#x5219;&#x5728;&#x539F;&#x6709;&#x7684;&#x63CF;&#x8FF0;&#x4E2D;&#x589E;&#x52A0;&#x5BF9; <code>second</code> &#x7684;&#x63CF;&#x8FF0;&#x3002;</p>
<p>&#x56E0;&#x4E3A; <code>BlackjackCard</code> &#x662F;&#x4E00;&#x4E2A;&#x6CA1;&#x6709;&#x81EA;&#x5B9A;&#x4E49;&#x6784;&#x9020;&#x5668;&#x7684;&#x7ED3;&#x6784;&#x4F53;&#xFF0C;&#x5728; <a href="14_Initialization.html#memberwise_initializers_for_structure_types">&#x7ED3;&#x6784;&#x4F53;&#x7684;&#x9010;&#x4E00;&#x6210;&#x5458;&#x6784;&#x9020;&#x5668;</a> &#x4E2D;&#x53EF;&#x77E5;&#xFF0C;&#x7ED3;&#x6784;&#x4F53;&#x6709;&#x9ED8;&#x8BA4;&#x7684;&#x6210;&#x5458;&#x6784;&#x9020;&#x5668;&#xFF0C;&#x6240;&#x4EE5;&#x4F60;&#x53EF;&#x4EE5;&#x7528;&#x9ED8;&#x8BA4;&#x7684;&#x6784;&#x9020;&#x5668;&#x53BB;&#x521D;&#x59CB;&#x5316;&#x65B0;&#x5E38;&#x91CF; <code>theAceOfSpades</code>&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> theAceOfSpades = <span class="hljs-type">BlackjackCard</span>(rank: .ace, suit: .spades)
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;theAceOfSpades: <span class="hljs-subst">\(theAceOfSpades.description)</span>&quot;</span>)
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;theAceOfSpades: suit is &#x2660;, value is 1 or 11&#x201D;</span>
</code></pre>
<p>&#x5C3D;&#x7BA1; <code>Rank</code> &#x548C; <code>Suit</code> &#x5D4C;&#x5957;&#x5728; <code>BlackjackCard</code> &#x4E2D;&#xFF0C;&#x4F46;&#x5B83;&#x4EEC;&#x7684;&#x7C7B;&#x578B;&#x4ECD;&#x53EF;&#x4ECE;&#x4E0A;&#x4E0B;&#x6587;&#x4E2D;&#x63A8;&#x65AD;&#x51FA;&#x6765;&#xFF0C;&#x6240;&#x4EE5;&#x5728;&#x521D;&#x59CB;&#x5316;&#x5B9E;&#x4F8B;&#x65F6;&#x80FD;&#x591F;&#x5355;&#x72EC;&#x901A;&#x8FC7;&#x6210;&#x5458;&#x540D;&#x79F0;&#xFF08;<code>.ace</code> &#x548C; <code>.spades</code>&#xFF09;&#x5F15;&#x7528;&#x679A;&#x4E3E;&#x5B9E;&#x4F8B;&#x3002;&#x5728;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;<code>description</code> &#x5C5E;&#x6027;&#x6B63;&#x786E;&#x5730;&#x53CD;&#x6620;&#x4E86;&#x9ED1;&#x6843; A &#x724C;&#x5177;&#x6709; <code>1</code> &#x548C; <code>11</code> &#x4E24;&#x4E2A;&#x503C;&#x3002;</p>
<h2 id="referring-to-nested-types">&#x5F15;&#x7528;&#x5D4C;&#x5957;&#x7C7B;&#x578B; </h2>
<p>&#x5728;&#x5916;&#x90E8;&#x5F15;&#x7528;&#x5D4C;&#x5957;&#x7C7B;&#x578B;&#x65F6;&#xFF0C;&#x5728;&#x5D4C;&#x5957;&#x7C7B;&#x578B;&#x7684;&#x7C7B;&#x578B;&#x540D;&#x524D;&#x52A0;&#x4E0A;&#x5176;&#x5916;&#x90E8;&#x7C7B;&#x578B;&#x7684;&#x7C7B;&#x578B;&#x540D;&#x4F5C;&#x4E3A;&#x524D;&#x7F00;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> heartsSymbol = <span class="hljs-type">BlackjackCard</span>.<span class="hljs-type">Suit</span>.hearts.rawValue
<span class="hljs-comment">// &#x7EA2;&#x5FC3;&#x7B26;&#x53F7;&#x4E3A;&#x201C;&#x2661;&#x201D;</span>
</code></pre>
<p>&#x5BF9;&#x4E8E;&#x4E0A;&#x9762;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#xFF0C;&#x8FD9;&#x6837;&#x53EF;&#x4EE5;&#x4F7F; <code>Suit</code>&#x3001;<code>Rank</code> &#x548C; <code>Values</code> &#x7684;&#x540D;&#x5B57;&#x5C3D;&#x53EF;&#x80FD;&#x7684;&#x77ED;&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x4EEC;&#x7684;&#x540D;&#x5B57;&#x53EF;&#x4EE5;&#x7531;&#x5B9A;&#x4E49;&#x5B83;&#x4EEC;&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#x6765;&#x9650;&#x5B9A;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="18_Type_Casting.html" class="navigation navigation-prev " aria-label="Previous page: 类型转换">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="20_Extensions.html" class="navigation navigation-next " aria-label="Next page: 扩展">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"嵌套类型","level":"1.3.19","depth":2,"next":{"title":"扩展","level":"1.3.20","depth":2,"path":"chapter2/20_Extensions.md","ref":"chapter2/20_Extensions.md","articles":[]},"previous":{"title":"类型转换","level":"1.3.18","depth":2,"path":"chapter2/18_Type_Casting.md","ref":"chapter2/18_Type_Casting.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"chapter2/19_Nested_Types.md","mtime":"2019-07-05T01:01:08.118Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-07-05T02:41:17.672Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src=""></script>
    <script src=""></script>
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    

    </body>
</html>

